#include<iostream>
#include<opencv2/imgcodecs.hpp>
#include<opencv2/highgui.hpp>
#include<opencv2/imgproc.hpp>
#include<opencv2/objdetect.hpp>
#include<opencv2/opencv.hpp>
#include<opencv2/ximgproc.hpp>
#include<string>
#include<vector>
#include<fstream>
#include <fcntl.h>
#include <termios.h>
#include"func.h"

using namespace std;
using namespace cv;



Result recognize(const Mat& target_bin_img, const Mat& toward_bin_img, Mat& img) 
{
    Result result;

    // // 目标识别
    // vector<vector<Point>> contours;
    // findContours(target_bin_img, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);

    // 激光点识别
    vector<vector<Point>> contours2;
    findContours(toward_bin_img, contours2, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);

    
    // if (contours.empty()) 
    // {
    //     result = {0, 0, 0, 0};
    //     cout << "No target detected." << endl;
    //     imshow("img", img);
    //     return result;
    // }

    if (contours2.empty()) 
    {
        result = {0, 0, 0, 0};
        cout << "No laser point detected." << endl;
        imshow("img", img);
        return result;
    }

    else
    {
        // // 目标识别以及中心点计算
        // double maxArea = 0;
        // int maxContourIdx = -1;
        // for (int i = 0; i < contours.size(); i++) 
        // {
        //     double area = contourArea(contours[i]);
        //     if (area > 1000) 
        //     {
        //         maxArea = area;
        //         maxContourIdx = i;
        //     }
        // }
       

        // 激光点识别以及中心点计算
        double maxArea2 = 0;
        int minContourIdx = -1;
        for (int i = 0; i < contours2.size(); i++) 
        {
            double area = contourArea(contours2[i]);
            if (area > 200) 
            {
                maxArea2 = area;
                minContourIdx = i;
            }
        }
        // if(maxContourIdx==-1||minContourIdx==-1)
        if(minContourIdx==-1)
        {
            result = {0, 0, 0, 0};
            cout << "No target or laesr detected." << endl;
            imshow("img", img);
            return result;
        }
        else
        {
            // Rect boundingRect1 = boundingRect(contours[maxContourIdx]);
            // rectangle(img, boundingRect1, Scalar(0, 255, 0), 2);
            // result.targetX = (boundingRect1.x + boundingRect1.width) / 2;
            // result.targetY = (boundingRect1.y + boundingRect1.height) / 2;

            Rect boundingRect2 = boundingRect(contours2[minContourIdx]);
            // cout<<boundingRect2.area()<<endl;
            rectangle(img, boundingRect2, Scalar(255, 0, 0), 2);
            // result.laserX = (boundingRect2.x + boundingRect2.width) / 2;
            // result.laserY = (boundingRect2.y + boundingRect2.height) / 2;

            // cout << "Target: (" << result.targetX << ", " << result.targetY << ")" << endl;
            cout << "Laser point: (" << result.laserX << ", " << result.laserY << ")" << endl;

            imshow("img", img);

            return result;
        }
    }
}
